import REQ from "@/api/web/REQ.ts";
import {Res, ResPage} from "@/api/objects";

export interface CreateFileBucketParam {
    name: string,
    rootPath: string
}

export interface FbInfo {
    id: string,
    name: string,
    rootPath: string,
    type: string,
    coverLink?: string,
    creatorId: string,
    available: boolean,
    createTime: string,
    updateTime: string
}

class FileBucketApi {
    /**
     * 分页获取文件容器信息
     * @param current 当前页码
     * @param size 单页数量
     * @param search 搜索
     * @param type 类型
     */
    pageFbInfo = async (current: number, size: number, search?: string, type?: string) => {
        return REQ<any, Res<ResPage<FbInfo>>>({
            method: "get",
            url: "/api/fb/info",
            params: {
                current,
                size,
                search,
                type,
            },
        })
    }

    /**
     * 通过id获取文件容器信息
     * @param id 文件容器id
     */
    getFbInfoById = async (id: string) => {
        return REQ<any, Res<FbInfo>>({
            method: 'get',
            url: '/api/fb/info/' + id,
        })
    }

    /**
     * 创建文件容器
     * @param data
     */
    createFileBucket = async (data: CreateFileBucketParam) => {
        return REQ<CreateFileBucketParam, Res<FbInfo>>({
            method: 'post',
            url: "/api/fb/create",
            data,
        })
    }

    /**
     * 删除文件容器
     * @param id
     */
    deleteFileBucket = async (id: string) => {
        return REQ<any, Res<void>>({
            method: "delete",
            url: "/api/fb/" + id,
        })
    }

    /**
     * 分页获取全部文件
     * @param id 文件容器id
     * @param current 页码
     * @param size 单页大小
     * @param contentTypePrefix 文件ContentType前缀，用于匹配指定文件类型
     */
    pageFiles = async (id: string, current: number, size: number, contentTypePrefix?: string) => {
        return REQ<any, Res<ResPage<string>>>({
            method: "get",
            url: "/api/fb/page/" + id,
            params: {
                current, size, contentTypePrefix,
            },
        })
    }

    /**
     * 修改文件容器类型
     */
    updateFbInfo = async (fbInfo: FbInfo) => {
        return REQ<any, Res<void>>({
            method: "post",
            url: "/api/fb/update",
            data: {
                ...fbInfo,
            },
        })
    }

    /**
     * 分页获取容器文件夹
     * @param {string} id 容器id
     * @param {string} current 分页大小
     * @param {string} size 页码
     * @returns
     */
    pageDirById = async (id: string, current: number, size: number) => {
        return REQ<any, Res<ResPage<string>>>({
            method: 'get',
            url: "/api/fb/page/dir/" + id,
            params: {
                current, size,
            },
        });
    }

    /**
     * 分页获取容器文件夹内的文件
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param current 虚拟文件夹页面
     * @param size 虚拟文件夹单页大小
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    pageFileInDirById = async (id: string, dirPath: string, current: number, size: number, contentTypePrefix?: string) => {
        return REQ<any, Res<ResPage<string>>>({
            method: 'get',
            url: "/api/fb/page/fileInDir/" + id,
            params: {
                dirPath, current, size, contentTypePrefix,
            },
        })
    }

    /**
     * 分页获取容器文件夹内的虚拟分页索引（第一个文件名）
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param current 虚拟文件夹页面
     * @param size 虚拟文件夹单页大小
     * @param minBreakSize 虚拟分页字符串匹配最小打断长度
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    pageVirtualPageIndex = async (
        id: string,
        dirPath: string,
        current: number,
        size: number,
        minBreakSize?: number,
        contentTypePrefix?: string,
    ) => {
        return REQ<any, Res<ResPage<string>>>({
            method: 'get',
            url: "/api/fb/page/vp/index/" + id,
            params: {
                dirPath, current, size, minBreakSize, contentTypePrefix,
            },
        })
    }

    /**
     * 通过虚拟分页索引获取文件容器中指定文件夹里索引的虚拟分页
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param index 虚拟分页索引（第一个文件路径）
     * @param current 虚拟文件夹页面
     * @param size 虚拟文件夹单页大小
     * @param minBreakSize 虚拟分页字符串匹配最小打断长度
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    pageVirtualPage = async (
        id: string,
        dirPath: string,
        index: string,
        current: number,
        size: number,
        minBreakSize?: number,
        contentTypePrefix?: string,
    ) => {
        return REQ<any, Res<ResPage<string>>>({
            method: 'get',
            url: "/api/fb/page/vp/" + id,
            params: {
                dirPath, index, current, size, minBreakSize, contentTypePrefix,
            },
        })
    }

    /**
     * 从文件夹中获取虚拟分页
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param current 虚拟文件夹页面
     * @param size 虚拟文件夹单页大小
     * @param virtualCurrent 虚拟分页文件页码（通过/分割数字）
     * @param virtualSize 虚拟分页文件单页大小（通过/分割数字）
     * @param minBreakSize 虚拟分页字符串匹配最小打断长度
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    pageVirtualPageInDir = async (
        id: string,
        dirPath: string,
        current: number,
        size: number,
        virtualCurrent?: string,
        virtualSize?: string,
        minBreakSize?: number,
        contentTypePrefix?: string,
    ) => {
        return REQ<any, Res<ResPage<ResPage<string>>>>({
            method: 'get',
            url: "/api/fb/page/virtualPage/" + id,
            params: {
                dirPath, current, size, virtualCurrent, virtualSize, minBreakSize, contentTypePrefix,
            },
        })
    }

    /**
     * 获取随机文件链接
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    getRandomFilePath = async (
        id: string,
        dirPath?: string,
        contentTypePrefix?: string,
    ) => {
        return REQ<any, Res<String>>({
            method: 'get',
            url: '/api/fb/random/path/' + id,
            params: {
                dirPath, contentTypePrefix,
            },
        })
    }

    /**
     * 获取随机文件
     * @param id 容器id
     * @param dirPath 目标文件夹
     * @param contentTypePrefix 指定查找的文件类型前缀
     */
    getRandomFile = async (
        id: string,
        dirPath?: string,
        contentTypePrefix?: string,
    ) => {
        return REQ<any, any>({
            method: 'get',
            url: '/api/fb/random/' + id,
            params: {
                dirPath, contentTypePrefix,
            },
        })
    }

    getRandomFileLink = (
        id: string,
        dirPath?: string,
        contentTypePrefix?: string,
    ) => {
        let path = "/api/fb/random/" + id
        if (dirPath || contentTypePrefix) {
            path += "?"
            if (dirPath) {
                path += "dirPath=" + dirPath + "&"
            }
            if (contentTypePrefix) {
                path += "contentTypePrefix=" + contentTypePrefix + "&"
            }
        }
        return path
    }
}

export default new FileBucketApi()
